package com.auxgroup.smarthome.controller;

import com.auxgroup.smarthome.basebean.MenuTypeEnum;
import com.auxgroup.smarthome.dto.LoginDto;
import com.auxgroup.smarthome.filter.PermissionSecurityUserService;
import com.auxgroup.smarthome.filter.annotation.PermissionFilter;
import com.auxgroup.smarthome.filter.annotation.PermissionMenu;
import com.auxgroup.smarthome.service.UserService;
import com.auxgroup.smarthome.session.SessionUser;
import com.auxgroup.smarthome.vo.LoginVo;
import com.auxgroup.smarthome.web.ApiResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * Created by kevinchen on 2016/12/15.
 */

@Api( tags = "系统登录")
@RestController
public class LoginController {

    @Autowired
    private UserService userService;
    @Autowired
    private PermissionSecurityUserService permissionSecurityUserService;

    @ApiOperation(value = "系统登录", notes = "系统用户登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userName", value = "登录用户名",required = true,paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "password", value = "密码", required = true, paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "captchaCode", value = "验证码", paramType = "form", dataType = "string")
    })
    @PostMapping(value = "/login")
    @PermissionFilter(filter = true)
    public ApiResponse<LoginVo> login(@ApiIgnore @Valid LoginDto loginDto){
        return userService.login(loginDto);
    }

    @ApiOperation(value = "注销", notes = "系统用户退出")
    @PostMapping(value = "/loginout")
    @PermissionFilter(filter = true)
    public ApiResponse loginOut(){
        return userService.logout();
    }

    @GetMapping(value = "/alogin")
    @PermissionMenu(id = "upms_alogin", parentId = "umps_menu_menu_manage", name = "菜单导航栏", menuType = MenuTypeEnum.BUTTON)
    public ApiResponse<SessionUser> alogin(){
        return ApiResponse.ok(permissionSecurityUserService.getUserEntity());
    }

    @ApiOperation(value = "修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "oldPass", value = "旧密码",required = true,paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "newPass", value = "新密码",required = true,paramType = "form", dataType = "string"),
    })
    @PutMapping(value = "/change_password")
    public ApiResponse modifyPassword(@RequestParam("oldPass") String oldPass,@RequestParam("newPass")  String newPass) {
       return userService.modifyPassword(oldPass,newPass);
    }

    @ApiOperation(value = "发送重置密码邮件")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "username", value = "用户名",required = true,paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "email", value = "邮箱",required = true,paramType = "form", dataType = "string"),
    })
    @PostMapping(value = "/forget_password_token")
    @PermissionFilter(filter = true)
    public ApiResponse forgetPassword(@RequestParam("username") String username, @RequestParam("email") String email) {
        String callbackUrl = "https://smarthome.auxgroup.com/login.html#/reset/";
        return userService.forgetPassword(callbackUrl, username, email);
    }

    @ApiOperation(value = "重置密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "邮件中收到的临时token",required = true,paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "username", value = "账号",required = true,paramType = "form", dataType = "string"),
            @ApiImplicitParam(name = "newPass", value = "新密码",required = true,paramType = "form", dataType = "string"),
    })
    @PutMapping(value = "/reset_password")
    @PermissionFilter(filter = true)
    public ApiResponse validForgetPasswordToken(@RequestParam("token") String token, @RequestParam("username") String username, @RequestParam("newPass") String newPass) {
        return userService.validForgetPasswordToken(token, username, newPass);
    }

    private final String RECOVER_TOKEN = "Aux-recover-private";
    @ApiIgnore
    @GetMapping(value = "/recover-account")
    @PermissionFilter(filter = true)
    public ApiResponse recoverAccount(String token, String userName){
        if(!RECOVER_TOKEN.equals(token)) {
            return ApiResponse.prompt(-1000, "无效的token");
        }
        return userService.recoverAccount(userName);
    }

}
